package com.threeonemeeting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.snda.location.CallBack;
import com.snda.location.LocationManager;
import com.snda.location.basetype.Address;
import com.threeonemeeting.Const;
import com.threeonemeeting.LocationService;
import com.threeonemeeting.R;
import com.threeonemeeting.activity.ext.MeetingPageListActivity;
import com.threeonemeeting.listview.DragRefreshListView;
import com.threeonemeeting.task.GetNearByMeetingListTask;

public class NearByMeetingListActivity extends MeetingPageListActivity {

	public static final String ACTION_PRINT_LOCATION = "location_service_print";
	public static final String ACTION_START_LOCATION = "location_service_start";
	public static final String ACTION_UPLOAD_LOG = "location_upload_log";

	private Intent serviceIntent;

	private double nLongitute;
	private double nLatitute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_meeting_list, "您附近的会议");

		TextView loadText = (TextView) findViewById(R.id.loading_text);
		loadText.setText("正在定位您的位置...");

		meetingListView
				.setOnRefreshListener(new DragRefreshListView.OnRefreshListener() {
					public void onRefresh() {

						// 发送刷新请求
						GetNearByMeetingListTask task = new GetNearByMeetingListTask(
								NearByMeetingListActivity.this
										.getApplicationContext(),
								NearByMeetingListActivity.this);
						Bundle params = new Bundle();
						params.putString("Latitude", String.valueOf(nLatitute));
						params.putString("Longitude",
								String.valueOf(nLongitute));
						TaskManager.addTask(task, params, false);
					}
				});

		meetingListView
				.setOnLoadMoreListener(new DragRefreshListView.OnLoadMoreListener() {

					public void onLoadMore(int nNextPage) {
						loadMoreNearByMeeting(nNextPage);

						showLoadingMoreLayout(true);
					}
				});

		MessageBox.showToast(getApplicationContext(), "开始定位您的位置，请稍候...");

		// 启动定位Service
		serviceIntent = new Intent();
		serviceIntent.setAction(ACTION_START_LOCATION);
		serviceIntent.setClass(getApplicationContext(), LocationService.class);
		getApplicationContext().startService(serviceIntent);

		LocationManager.getCurrentLocation(getApplicationContext(),
				new CallBack() {
					public void onReceiveAddress(Address addr) {
						Log.d(Const.Tag, addr.mAddress);

						nLongitute = addr.mLng;
						nLatitute = addr.mLat;

						getApplicationContext().stopService(serviceIntent);

						TextView loadText = (TextView) findViewById(R.id.loading_text);
						loadText.setText("您的位置是" + addr.mAddress + "，正在加载附近的会议");

						loadMoreNearByMeeting(1);
					}
				});
	}

	private void loadMoreNearByMeeting(int nPage) {

		GetNearByMeetingListTask task = new GetNearByMeetingListTask(
				NearByMeetingListActivity.this.getApplicationContext(),
				NearByMeetingListActivity.this);
		task.setPages(nPage);
		Bundle params = new Bundle();
		params.putString("Latitude", String.valueOf(nLatitute));
		params.putString("Longitude", String.valueOf(nLongitute));
		TaskManager.addTask(task, params, false);
	}
}
